package com.mj.listen1._05二叉搜索树;

import com.mj.listen1._05二叉搜索树.printer.BinaryTrees;

import java.util.Comparator;

public class Main {
    static void test1() {
        Integer[] integer = new Integer[]{
                7, 4, 9, 2, 5, 8, 11, 3
        };

        BinarySearchTree<Integer> tree = new BinarySearchTree<>();
        for (int i = 0; i < integer.length; i++) {
            tree.add(integer[i]);
        }

        tree.add(1);
        tree.add(12);

        BinaryTrees.println(tree);
    }

    static void test2() {
        Integer[] integer = new Integer[]{
                7, 4, 9, 2, 5, 8, 11, 3
        };

        BinarySearchTree<Person> tree = new BinarySearchTree<>();
        for (int i = 0; i < integer.length; i++) {
            tree.add(new Person(integer[i]));
        }

        tree.add(new Person(1));
        tree.add(new Person(12));

        BinaryTrees.println(tree);
    }

    static void test3() {
        Integer[] integer = new Integer[]{
                7, 4, 9, 2, 5, 8, 11, 3
        };

        BinarySearchTree<Person> tree = new BinarySearchTree<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.getAge() - o1.getAge();
            }
        });

        for (int i = 0; i < integer.length; i++) {
            tree.add(new Person(integer[i]));
        }

        tree.add(new Person(1));
        tree.add(new Person(12));

        BinaryTrees.println(tree);
    }

    static void test4() {
        BinarySearchTree<Person> tree = new BinarySearchTree<>();

        tree.add(new Person(10, "howard"));
        tree.add(new Person(12, "tom"));
        tree.add(new Person(6, "lbj"));

        tree.add(new Person(10, "cobe"));

        BinaryTrees.println(tree);
    }

    static void test5() {
        Integer[] integer = new Integer[]{
                7, 4, 9, 2, 5, 8, 11, 3
        };

        BinarySearchTree<Integer> tree = new BinarySearchTree<>();
        for (int i = 0; i < integer.length; i++) {
            tree.add(integer[i]);
        }

        tree.add(1);
        tree.add(12);

        BinaryTrees.println(tree);

        // 7 4 2 1 3 5 9 8 11 12
        tree.preorderTraversal();// 前序遍历
        System.out.println("-------------");
        // 1 3 2 5 4 8 12 11 9 7
        tree.postorderTraversal();// 后序遍历
        System.out.println("-------------");
        // 1 2 3 4 5 7 8 9 11 12
        tree.inorderTraversal();// 中序遍历
    }

    static void test6() {
        Integer[] integer = new Integer[]{
                7, 4, 9, 2, 5, 8, 11, 3
        };

        BinarySearchTree<Integer> tree = new BinarySearchTree<>();
        for (int i = 0; i < integer.length; i++) {
            tree.add(integer[i]);
        }

        tree.add(1);
        tree.add(12);

        BinaryTrees.println(tree);

        // 层序遍历
        tree.levelOrderTranversal();
    }

    static void test7() {
        Integer[] integer = new Integer[]{
                7, 4, 9, 2, 5, 8, 11, 3
        };

        BinarySearchTree<Integer> tree = new BinarySearchTree<>();
        for (int i = 0; i < integer.length; i++) {
            tree.add(integer[i]);
        }

        tree.add(1);
        tree.add(12);

        BinaryTrees.println(tree);

        System.out.println(tree);// 调用 BinarySearchTree 的toString方法
    }

    // 获取二叉搜索树的高度
    static void test8() {
        Integer[] integer = new Integer[]{
                7, 4, 9, 2, 5, 8, 11, 3, 22, 21, 18
        };

        BinarySearchTree<Integer> tree = new BinarySearchTree<>();
        for (int i = 0; i < integer.length; i++) {
            tree.add(integer[i]);
        }

        tree.add(1);
        tree.add(12);

        BinaryTrees.println(tree);

        System.out.println(tree.height());
    }

    // 判断是否是完全二叉树
    static void test9() {
        Integer[] integer = new Integer[]{
                7, 4, 9, 2, 5, 8, 11
        };

        BinarySearchTree<Integer> tree = new BinarySearchTree<>();
        for (int i = 0; i < integer.length; i++) {
            tree.add(integer[i]);
        }

        BinaryTrees.println(tree);

        System.out.println(tree.isCompleteTree());
    }

    // 测试删除完全二叉树
    static void test10() {
        Integer[] integer = new Integer[]{
                7, 4, 9, 2, 5, 8, 11, 3, 13
        };

        BinarySearchTree<Integer> tree = new BinarySearchTree<>();
        for (int i = 0; i < integer.length; i++) {
            tree.add(integer[i]);
        }

        BinaryTrees.println(tree);

        // 删除
        tree.remove(9);
        tree.remove(4);


        BinaryTrees.println(tree);
    }

    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
//        test4();
//        test5();
//        test6();
//        test7();
//        test8();
//        test9();
        test10();
    }
}
